package com.tacuati.content;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.ImageIcon;

import org.json.JSONArray;
import org.json.JSONObject;

import com.tacuati.Utils;
import com.tacuati.exceptions.CollectionsFileNotFoundException;
import com.tacuati.ui.ContentTreeNode;

public class Content implements ContentTreeNode {
	private List<Collection> collections;
	private List<Item> itemsWithoutCollection;

	public void getContent(String pathCollectionsFile, String rootPathItems) throws CollectionsFileNotFoundException
		{
		collections.clear();
		itemsWithoutCollection.clear();

		Map<String, Item> items = Item.getItems(rootPathItems);
		Map<String, Item> itemsWithoutCollectionMap = Item.getItems(rootPathItems);
		
		try {
			String jsonText = Utils.readFileAsString(pathCollectionsFile);
			JSONObject json = new JSONObject(jsonText);
			Iterator it = json.keys();
			while (it.hasNext()) {
				String collectionJsonString = it.next().toString();
				Collection collection = new Collection(collectionJsonString);

				JSONArray items2 = (JSONArray) ((JSONObject) json.get(collectionJsonString)).get("items");

				String lastAccess = ((JSONObject) json.get(collectionJsonString)).get("lastAccess").toString();
				collection.setLastAccess(lastAccess);

				for (int i = 0; i < items2.length(); i++) {
					String h = items2.get(i).toString();
					String h2 = h.substring(1);
					if (h2.lastIndexOf('^') > 0) {
						h2 = h2.substring(0, h2.lastIndexOf('^'));
					}
					Item b = items.get(h2);

					if (b==null){ // If not exist
						b = new OrphanItem(h);
					}
					collection.getItems().add(b);
					itemsWithoutCollectionMap.remove(h2);						
				}
				collections.add(collection);
			}

			// Items without collection
			for (String h : itemsWithoutCollectionMap.keySet()) {
				itemsWithoutCollection.add(items.get(h));
			}
		} catch (FileNotFoundException fnfe){
			throw new CollectionsFileNotFoundException(fnfe);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Content() {
		collections = new ArrayList<Collection>();
		itemsWithoutCollection = new ArrayList<Item>();
	}

	public List<Collection> getCollections() {
		return collections;
	}

	public void setCollections(List<Collection> collections) {
		this.collections = collections;
	}

	public List<Item> getItemsWithoutCollection() {
		return itemsWithoutCollection;
	}

	public void setItemsWithoutCollection(List<Item> itemsWithoutCollection) {
		this.itemsWithoutCollection = itemsWithoutCollection;
	}

	public String toJson() {
		String json = "{";
		int i = 0;
		for (Collection collection : collections) {
			if (i > 0) {
				json += ",";
			}
			json += collection.toJson();
			i++;
		}
		json += "}";
		return json;
	}

	public boolean isJsonValid() {
		try {
			new JSONObject(toJson());
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public void sort() {
		Collections.sort(collections);
		Collections.sort(itemsWithoutCollection);
	}
	
	public void deleteOrphansItems(){
		for (Collection collection : getCollections()) {
			for (Item item : collection.getItems()) {
				if (item instanceof OrphanItem){
					collection.getItems().remove(item);
				}
			}
		}
	}

	@Override
	public ContentTreeNode getChild(int index) {
		if (index > collections.size() - 1) {
			return itemsWithoutCollection.get(index - collections.size());
		} else {
			return collections.get(index);
		}
	}

	@Override
	public int getChildCount() {
		return collections.size() + itemsWithoutCollection.size();
	}

	@Override
	public int getIndexOfChild(ContentTreeNode child) {
		if (child instanceof Collection) {
			return collections.indexOf(child);
		} else {
			return collections.size() + itemsWithoutCollection.indexOf(child);
		}
	}

	@Override
	public String getNodeName() {
		return "";
	}

	@Override
	public void setNodeName(String name) {
	}

	@Override
	public void addChild(int index, ContentTreeNode o) {
		if (index > collections.size() - 1) {
			System.out.println(index - collections.size());
			itemsWithoutCollection.add(index - collections.size(),(Item) o);
		} else {
			collections.add(index,(Collection) o);
		}
	}

	@Override
	public void removeChild(int index) {
		if (index > collections.size() - 1) {
			itemsWithoutCollection.remove(index - collections.size());
		} else {
			collections.remove(index);
		}
	}
	
	@Override
	public boolean isEditable() {
		return false;
	}
	
	@Override
	public ImageIcon getIcon() {
		return null;
	}

	@Override
	public String getNodeRenderer() {
		return "";
	}

	public void clear() {
		collections.clear();
		itemsWithoutCollection.clear();
	}
}
